﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Configuration;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.OleDb;
using DbSupportLib;
using Contributor.Data_Objects;
using DevExpress.Xpf.Grid;

namespace Contributor
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Loaded += MainWindow_Loaded;
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {

            string dbLocation = ConfigurationManager.AppSettings["DatabaseLocation"];


            AppConfig.ConnectionStr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False",
                dbLocation); 

            // Check that the database file exists
            if (!System.IO.File.Exists(dbLocation))
            {
                MessageBox.Show("Database not found at:" + dbLocation, "No Database");
                Application.Current.Shutdown();
                return;
            }


            try
            {
                gridControl1.ItemsSource = FindPeople.LoadData("");
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }


        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            string where = "";

            if (!string.IsNullOrWhiteSpace(txtSrchFirstName.Text))
            {
                where += String.Format("{1} FirstName LIKE '%{0}%'", txtSrchFirstName.Text.Trim(),
                    where.Length==0 ? "WHERE" : "AND" );
            }

            if (!string.IsNullOrWhiteSpace(txtSrchLastName.Text))
            {
                where += String.Format("{1} LastName LIKE '%{0}%'"
                    , txtSrchLastName.Text.Trim(),
                    where.Length==0 ? "WHERE" : "AND" );
            }


            gridSearch.ItemsSource = FindPeople.LoadSearchData(where);

        }

        private void tblviewSearch_RowDoubleClick(object sender, DevExpress.Xpf.Grid.RowDoubleClickEventArgs e)
        {
            GridCellMenuInfo menuInfo = tblviewSearch.GridMenu.MenuInfo as GridCellMenuInfo;
            if (e.HitInfo.InRow)
            {
                EntitySearch person = gridSearch.GetRow(e.HitInfo.RowHandle) as EntitySearch;
                if (person != null)
                {
                    tabControl.SelectTab(entityEditTab);
                    ucEntityDisplay1.LoadEntity(person);
                }
            }
        }





    }

}
